<template>
  <div class="login_box">
    <div class="back">
      <van-icon
        @click="$router.push('/find')"
        style="text-align: left"
        name="arrow-left"
        size="35"
      />
    </div>
    <div class="text">您好，请登录</div>
   <div class="mobile">
      <van-form>
        <van-field
          v-model="form.mobile"
          left-icon="phone-o"
          placeholder="请输入手机号"
          :rules="rules.mobile"
        />
      </van-form>
    </div>
    <div class="code">
      <van-form>
        <van-field
          v-model="form.code"
          left-icon="completed"
          placeholder="请输入验证码"
          :rules="rules.code"
        />
      </van-form>
      <button v-if="!timeShow" @click="getCode" class="getCode">获取验证码</button>
      <van-count-down @finish="finishTime" class="codeTime" v-if="timeShow" :time="time" format=" ss 秒后重试" />
    </div>
    <p class="arage">
      登录即同意
      <span>《用户使用协议》</span>
      和
      <span>《隐私协议》</span>
    </p>
    <van-button @click="login" round type="danger" size="large">确定</van-button>
  </div>
</template>

<script>
import { getCodeAPI } from '@/api/user'
export default {
  name: 'UserLogin',
  data () {
    return {
      timeShow: false,
      form: {
        mobile: '18888881111',
        code: ''
      },
      rules: {
        mobile: [{ required: true, message: '请输入手机号', trigger: 'onBlur' }],
        code: [{ required: true, message: '请输入验证码', trigger: 'onBlur' }]
      },
      time: 60 * 1000
    }
  },
  methods: {
    finishTime () {
      this.timeShow = false
    },
    async getCode () {
      this.timeShow = true
      const res = await getCodeAPI({
        mobile: this.form.mobile
      })
      this.$toast.success(res.data)
    },
    async login () {
      try {
        this.$store.dispatch('user/login', this.form)
        this.$router.push('/find')
      } catch (error) {

      }
    }
  }
}
</script>

<style lang="less" scoped>
.login_box {
  padding: 10px 10px;
  background-color: #f9f9f9;
  .back {
    display: flex;
    width: 100%;
    // background-color: #fff;
  }
  .text {
    width: 100%;
    height: 150px;

    text-align: left;
    line-height: 150px;
    padding: 10px 10px;
    font-size: 18px;
    font-weight: 700;
  }
  .mobile{
    width: 100%;
    margin-bottom: 5px;
  }
  .code{
    display: flex;
    position: relative;
    justify-content: space-between;
    .getCode{
      flex: 1;
      background-color: #f9f9f9;
      border: none;
      color: #00c7e4;
    }
    .codeTime{
      position: absolute;
      right: 20px;
      top: 15px;
      font-size: 16px;
      color: #00c7e4;
    }
  }
  .arage{
    margin-bottom: 40px;
    text-align: left;
    font-size: 12px;
    span{
      color: #00c7e4;
    }
  }
}
</style>
